package com.bw.permission.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.permission.config.AjaxResult;
import com.bw.permission.entity.Menu;
import com.bw.permission.entity.RoleMenu;
import com.bw.permission.service.IMenuService;
import com.bw.permission.service.IRoleMenuService;
import com.bw.permission.utils.JwtUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhanggm
 * @since 2020-10-28
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private IMenuService iMenuService;

    @Autowired
    private IRoleMenuService iRoleMenuService;

    @Autowired
    private JwtUtils jwtUtils;


    @RequestMapping("/getPageInfo")
    public AjaxResult getPageInfo(Menu menu,
                                  @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize",defaultValue = "3") Integer pageSize){
        PageInfo<Menu> pageInfo = iMenuService.getPageInfo(menu, pageNum, pageSize);
        return AjaxResult.ok(pageInfo);
    }

    @PostMapping("save")
    public AjaxResult save(@RequestBody Menu menu){
        //设置创建时间
        if(menu.getId()==null){
            menu.setCreateTime(LocalDateTime.now());
        }
        //所属菜单切换，造成组件和路径输入项有不合法数据的bug
        if(menu.getParentId()==0){
            menu.setComponent(null);
            menu.setPath(null);
        }
        boolean b = iMenuService.saveOrUpdate(menu);
        return AjaxResult.ok(b);
    }


    @GetMapping("getMenuListByPid")
    public AjaxResult getMenuListByPid(Integer pid){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parentId",pid);
        return AjaxResult.ok(iMenuService.list(queryWrapper));
    }


    /**
     * 根据登录名称，查询菜单权限
     * @param request
     * @return
     */
    @GetMapping("getTreeMenus")
    public AjaxResult getMenuTreeByUsername(HttpServletRequest request){
        //token存放的是用登录名
        String username = jwtUtils.getCurUsername();
        //根据登录名，查询菜单信息
        List<Menu> menus = iMenuService.getMenuTreeByUsername(username);
        return AjaxResult.ok(menus);
    }

    /**
     * 查询所有菜单数据
     * @return
     */
    @GetMapping("getAllTreeMenus")
    public AjaxResult getMenuTreeByUsername(){
        //根据登录名，查询菜单信息
        List<Menu> menus = iMenuService.getMenuTreeByUsername("admin");
        return AjaxResult.ok(menus);
    }

    /**
     * 根据pid查询菜单列表
     * @param parentId
     * @return
     */
    @GetMapping("getMenuListByParentId")
    public AjaxResult getMenuListByParentId(Integer parentId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parentId","0");
        List list = iMenuService.list(queryWrapper);
        return AjaxResult.ok(list);
    }


    @GetMapping("getMenuIdsByRoleId")
    public AjaxResult getMenuIdsByRoleId(String roleId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("rid",roleId);
        List<RoleMenu> list = iRoleMenuService.list(queryWrapper);
        List<Integer> menuIds = list.stream().map(rm -> rm.getMid()).collect(Collectors.toList());
        if(menuIds==null || menuIds.size()==0){
            return AjaxResult.ok(new ArrayList<>());
        }
        //父菜单Id是否应该选中，如果未权限菜单，然后删除父菜单Id
        List<Menu> menus = iMenuService.listByIds(menuIds);
        //得到父菜单Ids
        Set<Integer> parentIds = menus.stream().map(m -> m.getParentId()).collect(Collectors.toSet());
        menuIds.removeAll(parentIds);
       /* menus.forEach(m->{
            if(m.getParentId()==0){
                //查询子菜单数量
                queryWrapper.clear();
                queryWrapper.eq("pid",m.getId());
                int count = iMenuService.count(queryWrapper);
                //当前选中的子菜单数量
                List<Menu> subMenuList = iMenuService.getMenuListByPid(m.getId(), menus);
                if(count!=subMenuList.size()){

                }
            }
        });*/
        return AjaxResult.ok(menuIds);
    }
}
